package com.xinsoft.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinsoft.common.exception.BDException;
import com.xinsoft.constant.Const;
import com.xinsoft.entity.dto.ScmOutsourcingFeedingItemParam;
import com.xinsoft.entity.dto.ScmOutsourcingFeedingParam;
import com.xinsoft.entity.po.ScmOutsourcingFeeding;
import com.xinsoft.entity.vo.ScmOutsourcingFeedingDetailVo;
import com.xinsoft.entity.vo.ScmOutsourcingFeedingItemVo;
import com.xinsoft.entity.vo.ScmOutsourcingFeedingPageVo;
import com.xinsoft.mapper.ScmOutsourcingFeedingItemMapper;
import com.xinsoft.service.ScmOutsourcingFeedingService;
import com.xinsoft.mapper.ScmOutsourcingFeedingMapper;
import com.xinsoft.utils.TokenUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author ZYM
 * @description 针对表【scm_outsourcing_feeding(委外投料)】的数据库操作Service实现
 * @createDate 2023-08-31 15:30:45
 */
@Service
public class ScmOutsourcingFeedingServiceImpl extends ServiceImpl<ScmOutsourcingFeedingMapper, ScmOutsourcingFeeding>
        implements ScmOutsourcingFeedingService {

    @Resource
    private ScmOutsourcingFeedingItemMapper outsourcingFeedingItemMapper;

    @Override
    public IPage<ScmOutsourcingFeedingPageVo> findPageListByParam(ScmOutsourcingFeedingParam param) {
        Page<ScmOutsourcingFeedingPageVo> page = new Page<>(param.getCurrentPage(), param.getPageSize());
        IPage<ScmOutsourcingFeedingPageVo> pageList = baseMapper.findPageListByParam(page, param);
        return pageList;
    }

    @Override
    public IPage<ScmOutsourcingFeedingItemVo> childList(ScmOutsourcingFeedingItemParam param) {
        Page<ScmOutsourcingFeedingItemVo> page = new Page<>(param.getCurrentPage(), param.getPageSize());
        return outsourcingFeedingItemMapper.childList(page, param);
    }

    @Override
    public void audit(Integer id) {
        ScmOutsourcingFeeding feeding = this.getById(id);
        if (Const.AUDIT_STATUS_NORMAL.equals(feeding.getAuditStatus())) {
            throw new BDException("该单据已审核，无需重复审核");
        }
        feeding.setAuditId(TokenUtil.getUserId());
        feeding.setAuditTime(new Date());
        feeding.setAuditStatus(Const.AUDIT_STATUS_NORMAL);
        this.updateById(feeding);
    }

    @Override
    public void reverseAudit(Integer id) {
        ScmOutsourcingFeeding feeding = this.getById(id);
        if (Const.AUDIT_STATUS_WAIT.equals(feeding.getAuditStatus())) {
            throw new BDException("该单据未审核，无需反审核");
        }
        int citeCount = baseMapper.getOutsourcingOutstockCiteCount(id);
        if (citeCount > 0) {
            throw new BDException("该单据已产生下游单据，不能反审核");
        }
        feeding.setAuditId(null);
        feeding.setAuditTime(null);
        feeding.setAuditStatus(Const.AUDIT_STATUS_WAIT);
        this.updateById(feeding);
    }

    @Override
    public ScmOutsourcingFeedingDetailVo detail(Integer id) {
        ScmOutsourcingFeedingDetailVo vo = baseMapper.getFeedingById(id);
        vo.setScmOutsourcingFeedingItemList(outsourcingFeedingItemMapper.getFeedingItemList(id));
        return vo;
    }
}




